package com.bp.hearthealth.pulse.lnapps.bp_view

import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import com.bp.hearthealth.pulse.lnapps.R

/**
 * @author isnoam
 * @package_name ElasticProgressBar
 * @description：
 */


class BPElasticProgressBar : View {
    private var backgroundPaint: Paint? = null
    private var progressPaint: Paint? = null
    private var progress = 0f
    private var elasticAnimator: ObjectAnimator? = null
    private var customColor = 0

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initAttrs(attrs)
        init()
    }

    private fun initAttrs(attrs: AttributeSet?) {
        if (attrs!=null){
            // 获取自定义属性
            val context = context
            val attributes =
                context.obtainStyledAttributes(attrs, R.styleable.ElasticProgressBar)
             customColor = attributes.getColor(R.styleable.ElasticProgressBar_customColor, Color.BLACK)
            attributes.recycle()
        } else {
            customColor = Color.BLACK
        }
    }

    private fun init() {
        backgroundPaint = Paint()
        backgroundPaint!!.color = Color.GRAY
        backgroundPaint!!.style = Paint.Style.FILL

        progressPaint = Paint()
        progressPaint!!.color = customColor
        progressPaint!!.style = Paint.Style.FILL
        progressPaint!!.strokeCap = Paint.Cap.ROUND


        // 初始化弹性动画
        elasticAnimator = ObjectAnimator.ofFloat(this, "progress", 0f, 1f, 0f)
        elasticAnimator!!.duration = 2000 // 设置动画时长
        elasticAnimator!!.interpolator = AccelerateDecelerateInterpolator() // 设置加速减速插值器
        elasticAnimator!!.repeatMode = ObjectAnimator.REVERSE // 设置反向重复
        elasticAnimator!!.repeatCount = ObjectAnimator.INFINITE // 设置无限重复
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        // 绘制背景
        canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), backgroundPaint!!)
        // 计算进度条宽度
        val progressBarWidth = width * progress
        // 绘制进度条
        canvas.drawRect(0f, 0f, progressBarWidth, height.toFloat(), progressPaint!!)
    }

    // 设置加载进度
    fun setProgress(progress: Float) {
        this.progress = progress
        invalidate() // 刷新视图
    }

    // 开始弹性动画
    fun startElasticAnimation() {
        if (elasticAnimator != null && !elasticAnimator!!.isRunning) {
            elasticAnimator!!.start()
        }
    }

    // 停止弹性动画
    fun stopElasticAnimation() {
        if (elasticAnimator != null && elasticAnimator!!.isRunning) {
            elasticAnimator!!.cancel()
            setProgress(0f) // 恢复进度
        }
    }
}
